package com.df.bpm.utils.businessViewerV2.informatization;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.res.V_A;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class V_requirement extends V_A<M_requirementBasicDto> {
    V_details detailsPanel;
    public SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public List<String> getColNames() {
        return Arrays.asList("ID", "序号", "状态","需求编号","需求名称","开发者ID","备注","开发者工号","开发者姓名","预期","结期","创建时间","更改时间");
    }

    @Override
    public List<String> getKeyCols() {
        return Arrays.asList("需求编号","需求名称","备注","开发者工号","开发者姓名","状态");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
        Main.hideCol(iTable,5);
    }

    @Override
    public void setDialogSize(int width, int height) {
        jd.setSize(50*unit, 40*unit);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,4,3,6,7,1,32,7,7,11,11,11,11));
    }

    @Override
    public Object[] getRowValues(M_requirementBasicDto row) {
        return new Object[]{row.getInformatizationRequirementIndex(),-1,row.getState(),row.getNumber(),row.getName(),
                row.getEmployeeListIndex(),row.getTips(),row.getDeveloperNumber(),row.getDeveloperName(),
                dateFormat1.format(row.getCompletionDateEstimate()),dateFormat1.format(row.getCompletionDate()),
                dateFormat.format(row.getCreateDate()),dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_requirementBasicDto> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
//                        System.out.println("Row " + row + "& Col "+col+" double clicked");
                        Integer requirementID= (Integer) iTable.getValueAt(row, 0);
                        if(col==1 && CommonUtils.varIsNotBlank(requirementID)){//不为空的双击代表选中
                            showDetails(requirementID);
                        }else{
                            if((col==2)||(col==3)||(col==4)||(col==6)){//编辑需求基本信息
                                int rows=C_requirement.editRequirement(iTable,row,list,col);
                                if(rows>0){
                                    try {
                                        search();
                                    } catch (Exception ex) {
                                        throw new RuntimeException(ex);
                                    }
                                }
                            }else if((col==7)||(col==8)){
                                try {
                                    C_requirement.changeDeveloper(iTable,row,list);
                                } catch (Exception ex) {
                                    throw new RuntimeException(ex);
                                }
                            }else if((col==9)||(col==10)){//更改预期、结期
                                try {
                                    String oldValue=""+iTable.getValueAt(row, col);
                                    String tips="请输入日期：";
                                    String newValue= (String) JOptionPane.showInputDialog(null, tips, "日期", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
                                    if(newValue!=null && !newValue.equals(oldValue)){
                                        M_requirementDetailsDto temp=new M_requirementDetailsDto();
                                        temp.setBasic(new M_requirement());
                                        temp.getBasic().setInformatizationRequirementIndex(list.get(row).getInformatizationRequirementIndex());
                                        if(col==9){
                                            temp.getBasic().setCompletionDateEstimate(dateFormat1.parse(newValue));
                                        }else {
                                            temp.getBasic().setCompletionDate(dateFormat1.parse(newValue));
                                        }
                                        C_requirement.updateDetails(temp);
                                        search();
                                    }
                                } catch (Exception ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                        }

                    }
                }
            }
        };
    }

    private void showDetails(Integer requirementID) {
        if(detailsPanel==null){
            detailsPanel=new V_details(this.jp,unit);
        }
        detailsPanel.setVisible(false);
        M_requirementDetailsDto detailsDto=C_requirement.getDetails(requirementID);
        detailsPanel.putModel(detailsDto);
        detailsPanelFlash();
    }

    private void detailsPanelFlash() {
        if(detailsPanel==null){
            detailsPanel=new V_details(jp,unit);
            detailsPanel.setVisible(false);
        }
        detailsPanel.setVisible(true);
    }

    @Override
    public M_requirementBasicDto getEmptyRow() {
        return new M_requirementBasicDto();
    }

    @Override
    public void search(List<JTextField> fields)  {
        List<M_requirementBasicDto> results= C_requirement.search(fields);
        this.putModel(results);
    }





}
